<?php

class language {
	
	private $_selectedLanguage;
	private $translationType;
	private $_rawData;
	public $_lang;
	

	public function __construct($inputData, $lang){
		// Load the language JSON object
		$this->_lang = json_decode( file_get_contents(SYS_LANGDIR . $lang . ".js"), true );
		$this->_selectedLanguage = $lang;

		if(is_array($inputData)){
			// The input is a set of language variables to translate.
			$this->translationType = "table";
		} else {
			$this->translationType = "template";
		}
		$this->_rawData = $inputData;
	}
	
	public function translate($forceType = ""){
	
		if($forceType != "") $this->translationType = $forceType;
	
		switch($this->translationType){
		
			case "template":
				$this->_rawData = preg_replace_callback("/<var id=[\"']([a-zA-Z0-9_]+)[\"'][\s]?(?:class=[\"']([a-zA-Z]+)[\"'])?.*?>(.*?)<\/var>/i", array($this, "replaceMatches"), $this->_rawData);
			break;
			
			case "table":
				$this->createTranslationMap($this->_rawData);
			break;
		}
	}

	private function replaceMatches($matches){
		// $matches[0] = overall match, need to replace this
		// $matches[1] = ID. we need this to extract the information from the lang object.
		
		$type = explode("_", $matches[1]);
				
		switch($type[0]){
		
			// Internal translation from langmap
			case "lang":
				
				// Get the key for the language map item
				preg_match("/([^_\D]+)/", $matches[1], $results);
				
				if($this->_lang['map'] == NULL) return "[TransErr_" . $results[1] . "]";
				if(array_key_exists($matches[1], $this->_lang['map'])){
					// Text transforms
					switch($matches[2]){
				
						case "ucfirst":
							return ucfirst($this->_lang['map'][$matches[1]]);
						break;
				
						case "lower":
							return strtolower($this->_lang['map'][$matches[1]]);
						break;
				
						default:
							return $this->_lang['map'][$matches[1]];
						break;
					}
				} else {
					preg_match("/([^_\D]+)/", $matches[1], $results);
				
					return "[TransErr_" . $results[1] . "]";
				}
			
			break;
		
			// File translation (located in /system/languages/LANG/{file}.html)
			case "file":
				// Get the filename out of matches.
				
				$filename = implode("_", array_slice($type, 1));

				if(file_exists(SYS_LANGDIR . $this->_selectedLanguage . "/" . $filename . ".html")){
					return file_get_contents(SYS_LANGDIR . $this->_selectedLanguage . "/" . $filename . ".html");
				} else {
					preg_match("/([^_\D]+)/", $matches[1], $results);
				
					return "[TransErr_" . $results[1] . "]";
				}
			break;
		
		}
	}
	
	private function createTranslationMap(array $keys){
		$key_array = array_values($keys);
		$langMap = array();
		
		foreach($key_array as $key){
			if( array_key_exists($key, $this->_lang['map']) ){
				$langMap[$key] = $this->_lang['map'][$key];
			}
		}
		
		$this->_rawData = $langMap;
	}

	public function get(){
		return $this->_rawData;
	}
	
	public function getJSON(){
		if(is_array($this->_rawData)){
			return json_encode($this->_rawData);
		} else {
			throw new PicBoardException("Translation data is not an array: Uninitialised or in template mode.");
		}
	}
	
	public function getLanguageName(){
		return $this->_lang['descriptor']['friendlyName'];
	}
	
	
	/* Static functions available for calling outside of this class being init'd */

	public static function getAvailableLanguages(){
		$dirHandle = scandir(SYS_LANGDIR);
		
		$files = array_splice($dirHandle, 2);
		$langNames = array_map("util::filenameNoExt", $files);
		$map = array_combine($files, $langNames);
		
		
		$outputArr = array();
		
		foreach($files as $lang){
		
			$langFile = json_decode( file_get_contents(SYS_LANGDIR . $lang), true );
			$outputArr[ $map[$lang] ] = $langFile['descriptor']['friendlyName'];
		
		}
		
		return $outputArr;
	}

}

?>